package org.atf.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.atf.model.dao.AuditDAO;
import org.atf.model.dao.ProfileDAO;
import org.atf.model.dao.ServerDAO;
import org.atf.model.entity.Audit;
import org.atf.model.entity.Preference;
import org.atf.model.entity.Server;
import org.atf.model.entity.User;
import org.atf.utils.ATFUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;



public class ServerController {
	private ServerDAO serverDAO= new ServerDAO();
	private ProfileDAO profileDAO = new ProfileDAO();
	private AuditDAO auditDAO= new AuditDAO();
	private  static Logger  logger = Logger.getLogger(ServerController.class);
	public List<Server> getServerList(HttpServletRequest httpRequest, HttpSession session){
		// Need to add the server clean logic to support access
		logger.debug("Enter");
		List<Server> serverList= new ArrayList<Server>();
		List<Server> cleanedServerList = new ArrayList<Server>();
		//String user_header= ATFUtils.getAppUserHeader();
		//logger.debug("user_header: "+user_header);
		
		//String username=httpRequest.getHeader(user_header);
		//logger.info("username: "+username);
		
		//String groupStrHeader= ATFUtils.getAppGroupsHeader();
		//logger.debug("groupStrHeader: "+groupStrHeader);
		
		//String groupStr=httpRequest.getHeader(groupStrHeader);
		//logger.debug("groupStr: "+groupStr);
		User user=(User) session.getAttribute("ATFUser");
		if( user != null){
			logger.debug("User requested for server: "+user.getUsername());
			serverList=serverDAO.findAllServer();
			if(user.getAdmin().equalsIgnoreCase("true")){
				logger.info("Return all Servers");
				return serverList; 
			}
			else{
				
				if(user.getGroups() != null && !user.getGroups().equalsIgnoreCase("")){	
					List<String> groupNames=ATFUtils.parseAtfGroups(user.getGroups());
					for(Server s:serverList){
						String sName=s.getServername();
						
						if(ATFUtils.matchAppServerName(sName,groupNames)){
							logger.debug("Adding serverName: "+sName);
							cleanedServerList.add(s);
						}
					}
				}
			}
		}
		
		
			return cleanedServerList;
		
	}
	
	public List<Preference> getServerPreferencesList(Integer serverId){	
		return serverDAO.getPreference(serverId);
	}
	public String addPreference(Integer serverId,Preference preference){
		serverDAO.insertPreference(serverId,preference);
		return "OK";
	}
	
	public String deleteServerPreference(Integer serverId,Integer prefId){
		serverDAO.deletePreference(serverId, prefId);
		return "OK";
	}

	public HashMap<String, Object> getDeployOptions(Integer serverId){
		HashMap<String, Object> result= new HashMap<String, Object>();
		Server server=serverDAO.getServer(serverId);
		
		result.put("serverId", server.getId());
		result.put("servername", server.getServername());
		result.put("hostname", server.getHostname());
		
		Integer profileId=profileDAO.getProfileIdByServerId(serverId);
		result.put("profileId",profileId);
		result.put("profileName",profileDAO.getProfileName(profileId));
		result.put("region",profileDAO.getProfileRegion(profileId));
		result.put("profileHostname",profileDAO.getProfileHostname(profileId));
		result.put("deployFiles",ATFUtils.getDeployFilesList(profileId,server.getServername()));
		
		return result;	
	}
	
	
	public List<Audit> getAuditData(Integer serverId, Integer maxResults){
//		List<Audit> result =new ArrayList<Audit>();
//		result.add(new Audit("TEST", 1,"START", Calendar.getInstance().getTime(),Calendar.getInstance().getTime(),"Server STARTED"));
//		result.add(new Audit("TEST", 1,"STOP", Calendar.getInstance().getTime(),Calendar.getInstance().getTime(),"Server STOP"));
//		result.add(new Audit("TEST", 1,"DEPLOY", Calendar.getInstance().getTime(),Calendar.getInstance().getTime(),"Server DEPLOYED"));
//		result.add(new Audit("TEST", 1,"START", Calendar.getInstance().getTime(),Calendar.getInstance().getTime(),"Server STARTED"));
//		result.add(new Audit("TEST", 1,"STOP", Calendar.getInstance().getTime(),Calendar.getInstance().getTime(),"Server STOPPED"));
		
		return auditDAO.getAudits(serverId, maxResults);
	}
	public List<Audit> getAuditByServer(Integer serverId,Integer startIndex, Integer pageSize){
		return auditDAO.getAllAuditByServer(serverId, startIndex, pageSize);
	}
	
	
}
